//获取应用实例
const app = getApp();

Page({
  /**
  * 页面的初始数据
  */
  data: {
    currtab: 0,
    swipertab: [{ name: '爆款', index: 0 }, { name: '美妆', index: 1 }, { name: '家具', index: 2}
      , { name: '个护', index: 3 }, { name: '数码', index: 4 }, { name: '食品', index: 5 }],
    orderList1:[],
    orderList1isNull:false,
    orderList2:[],
    orderList2isNull:false,
    orderList3: [],
    orderList3isNull: false,
    orderList4: [],
    orderList4isNull: false,
    orderList5: [],
    orderList5isNull: false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.getOrder1();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow:function(){
    var that=this;
    console.log("onShow")
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },
  goodDetail: function (e) {
    console.log(app.globalData.token)
    if (app.globalData.token != '') {
      console.log(e);
      wx.navigateTo({
        url: 'item?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }

  },

//
  getOrder1: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that=this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/UserGoods/getList',
      data: {
        group_id:2,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        that.data.orderList1=[];
        for (var i = 0; i < res.data.data.list.length; i++) {
            if (res.data.data.list.length > 0) {
              console.log(res.data.data.list[i].img)
              res.data.data.list[i].img = app.globalData.imgUrl + "" + res.data.data.list[i].img.replace(/\\/g, "/");
              that.setData({
                orderList2: res.data.data.list
              })
            }
          }
        if (res.data.data.list.length==0){
          that.setData({
            orderList2isNull: true
          })
        }
        if (that.data.orderList2.length > 0) {
          that.setData({
            orderList2isNull: false
          })
        } else {
          that.setData({
            orderList2isNull: true
          })
        }
      }
    })
    
  },

  shopDetail: function (e) {
    console.log(app.globalData.token)
    if (app.globalData.token != '') {
      console.log(e);
      wx.navigateTo({
        url: '/pages/shop/vip_shop_detail?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }
  },
//获取收货地址列表
  getAddressList() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/ConsigneeAddress/getList',
      method: 'post',
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        'token': '' + app.globalData.token
      },
      success: (res) => {
        // console.log(res)
        if(res.data.data.list.length>0){
            for(var i=0;i<res.data.data.list.length;i++){
              if(res.data.data.list[i].is_default=='Y'){
                that.setData({
                  addressList:res.data.data.list[i],
                  isDefaultAddress:true
                })
              }
            }
          that.buy();
        }else{
          wx.showToast({
            title: '请先添加地址',
            icon:'none'
          })
          wx.navigateTo({
            url: '/pages/mine/setting-add_address',
          })
        }
      },
    });
  },
//立即购买
  orderBuy:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否要购买？',
      success: function (sm) {
        if (sm.confirm) {
          that.getAddressList();
          // 用户点击了确定 可以调用删除方法了
        } else if (sm.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
   
  },

  buy(){
    var that=this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/UserOrder/order',
      data:
      {
        token: app.globalData.token,
        goods_id: that.data.orderList2[0].goods_id,
        address_id: '' + that.data.addressList.id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        // console.log(res);
        if (res.data.code == 1) {
          that.getPayForm(res.data.data.order_id);
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }

      }, fail: function () {

      }
    });
  },


  //获取支付表单
  getPayForm(order_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/UserOrder/getPayForm',
      data:
      {
        token: app.globalData.token,
        order_id: order_id,
        pay_method: '' + + app.globalData.payfrom,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {

          wx.showToast({
            title: '购买成功',
            icon: 'success',
            duration: 2000
          })

          setTimeout(function () {
            //要延时执行的代码
            wx.navigateBack({
              delta: 1
            })
          }, 2000) //延迟时间 这里是1秒
          // "{"appid":"wx48126316bdbec25b","noncestr":"lk8qhri54a7stxryu88e4n5tuhgc0f7f","package":"Sign = WXPay","partnerid":"1498191702","prepayid":"wx23131631764983da394a941c1058073000","sign_type":"HMAC - SHA256","timestamp":1590210991,"sign":"F9B89B33AC715AABE0C69AC825E1104BA283335731A01C1C7200C13420E65FFF"}"

          // var tempJson = JSON.parse(res.data.data.pay_parameters)
          // console.log(tempJson)
          // console.log(tempJson.appid)
          // var packages = "prepay_id=" + tempJson.prepayid
          // console.log(packages)
          // wx.requestPayment(
          //   {
          //     'timeStamp': '' + tempJson.timestamp,
          //     'nonceStr': '' + tempJson.noncestr,
          //     'package': "" + packages,
          //     'signType': 'MD5',
          //     'paySign': '' + tempJson.sign,
          //     'success': function (res) {
          //       console.log(res)
          //     },
          //     'fail': function (res) {
          //       console.log(res)
          //     },
          //     'complete': function (res) {
          //       console.log(res)
          //     }
          //   })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }


      }, fail: function () {

      }, complete() {

      }
    });
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
});
